﻿using System;
using HttpWebServer;
using System.Threading;

namespace HWSConsole
{
    static class Program
    {
        static void Main(string[] args)
        {
            //var maximumThreads = 1;
            //var minimumThreads = 1;
            var hostName = "127.0.0.1";
            var port = 10080;
            var clientBuffer = 1024;

            if (args != null && args.Length == 5)
            {
                //maximumThreads = int.Parse(args[0]);
                //minimumThreads = int.Parse(args[1]);
                hostName = args[2];
                port = int.Parse(args[3]);
                clientBuffer = int.Parse(args[4]);
            }
            
            //ThreadPool.SetMaxThreads(maximumThreads, maximumThreads);
            //ThreadPool.SetMinThreads(minimumThreads, minimumThreads);

            var serverSide = new ServerSide(hostName, port, clientBuffer);
            var serverThread = new Thread(ServerStartThread);
            serverThread.Start(serverSide);

            Console.WriteLine("Server starts at \"{0}\" on port {1}.\n", hostName, port);
            Console.ReadLine();
            serverSide.ShutdownServerSide();
            serverThread.Join(10);
            Console.Write("\n\nBye-bye. ");
            Thread.Sleep(1000);
        }

        static void ServerStartThread(object ss)
        {
            ((ServerSide)ss).ClientRequestMessageEvent += ServerSideClientRequestMessageEvent;
            ((ServerSide)ss).ClientResponseMessageEvent += ServerSideClientResponseMessageEvent;
            ((ServerSide)ss).StartServer();        
        }

        static void ServerSideClientResponseMessageEvent(object sender, MessageEventArgs e)
        {
            Console.WriteLine(e.Message);
        }

        static void ServerSideClientRequestMessageEvent(object sender, MessageEventArgs e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
